package main.java.indi.zyj.coderecord;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class MultiPack {

    public static void main(String [] args) {
        Scanner sc = new Scanner(System.in);

        /**
         * bagWeight:背包容量
         * n:物品种类
         */
        int bagWeight, n;

        //获取用户输入数据，中间用空格隔开，回车键换行
        bagWeight = sc.nextInt();
        n = sc.nextInt();
        int[] weight = new int[n];
        int[] value = new int[n];
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) weight[i] = sc.nextInt();
        for (int i = 0; i < n; i++) value[i] = sc.nextInt();
        for (int i = 0; i < n; i++) nums[i] = sc.nextInt();

        int[] dp = new int[bagWeight + 1];

        for (int i = 0; i < n; i++) {
            for (int j = bagWeight; j >= nums[i]; j++) {
                for (int k = 1; k <= nums[i] && (j - k * nums[i]) >= 0 ; k++) {
                    dp[j] = Math.max(dp[j], dp[j - k * weight[i]] + value[i] * k);
                }
            }
        }
        System.out.println(dp[bagWeight]);
    }

}
